home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_pcdp / ada / pcmon.ada < prev    next >
Text File  |  1996-01-30  |  948b  |  46 lines

  1. package PC_Monitor is
  2.  
  3.   procedure Append(V: in  Integer);
  4.   procedure Take  (V: out Integer);
  5.  
  6. end PC_Monitor;
  7.  
  8. with Monitor_Package; use Monitor_Package;
  9. package body PC_Monitor is
  10.  
  11.   Not_Empty, Not_Full: Condition;
  12.  
  13.   N:        constant Integer := 10;
  14.  
  15.   Buffer:   array(0..N-1) of Integer;
  16.   In_Ptr,Out_Ptr:   Integer := 0;
  17.   Count:  Integer := 0;
  18.  
  19.   procedure Append(V: in Integer) is
  20.   begin
  21.     Monitor.Enter;
  22.     if Count = Buffer'Length then
  23.        Monitor.Leave;
  24.        Not_Full.Wait;
  25.     end if;
  26.     Buffer(In_Ptr) := V;
  27.     In_Ptr := (In_Ptr + 1) mod N;
  28.     Count := Count + 1;
  29.     Not_Empty.Signal;
  30.   end Append;
  31.  
  32.   procedure Take(V: out Integer) is
  33.   begin
  34.     Monitor.Enter;
  35.     if Count = 0 then
  36.       Monitor.Leave;
  37.       Not_Empty.Wait;
  38.     end if;
  39.     V := Buffer(Out_Ptr);
  40.     Out_Ptr := (Out_Ptr + 1) mod N;
  41.     Count := Count - 1;
  42.     Not_Full.Signal;
  43.   end Take;
  44.  
  45. end PC_Monitor;
  46.